package org.inspursc.s0517.health.evaluation.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.inspursc.s0517.health.common.util.ResponseUtil;
import org.inspursc.s0517.health.common.vo.ResponseVO;
import org.inspursc.s0517.health.evaluation.dto.SurveyDTO;
import org.inspursc.s0517.health.evaluation.dto.SurveyQuDetailDTO;
import org.inspursc.s0517.health.evaluation.service.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author B_Dream
 * @date 2018/5/24 15:19
 */
@RestController
@Api(description = "问卷API")
public class SurveyController {

    @Autowired
    SurveyService surveyService;


    @ApiOperation("增加调查问卷")
    @RequestMapping(value = "/saveSurvey", method = RequestMethod.POST)
    public ResponseEntity<ResponseVO> saveQuestionnaire(@RequestBody @Valid SurveyDTO survey, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
//            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
            return ResponseUtil.error400( bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            SurveyDTO dto=surveyService.saveSurvey(survey);
            return ResponseUtil.success("ok",dto);
        }catch (Exception e){
            return ResponseUtil.error(e.getMessage());
        }

    }

    @ApiOperation("修改调查问卷")
    @RequestMapping(value = "/updateSurvey", method = RequestMethod.PUT)
    public ResponseEntity<ResponseVO> updateSurvey(@RequestBody SurveyDTO survey) {
        try {
            SurveyDTO dto = surveyService.updateSurvey(survey);
            return ResponseUtil.success("ok", dto);
        }catch (Exception e){
            return ResponseUtil.error(e.getMessage());
        }
    }


    @ApiOperation("删除调查问卷")
    @RequestMapping(value = "/deleteSurvey", method = RequestMethod.DELETE)
    public ResponseEntity<ResponseVO> deleteQuestionnaire(@RequestParam(value = "suId") Integer suId) {
        try {
            Boolean is = surveyService.deleteSurvey(suId);
            if (is) {
                return ResponseUtil.success("删除成功！",null);
            } else {
                return ResponseUtil.error("删除失败!",null);
            }
        } catch (Exception e) {
            return ResponseUtil.error("系统异常:" + e.getMessage(),null);
        }

    }


    @ApiOperation("获取全部调查问卷")
    @RequestMapping(value = "/listSurvey", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> getQuestionnaire()  {
        try {
            List<SurveyDTO> dto = surveyService.findSurvey();
            return ResponseUtil.success("ok", dto);
        }catch (Exception e){
            return  ResponseUtil.error(e.getMessage());
        }
    }

    @ApiOperation(value = "根据权限获取问卷列表：查询出当前登录用户所属机构能够查看问卷列表\n")
    @GetMapping(value = "/findSurveysByOrgId")
    public ResponseEntity<ResponseVO> findSurveysByOrgId(HttpServletRequest request) {
        try {
            List<SurveyDTO> list = surveyService.findSurveyByOrgIdAndUserId(request);
            if (list != null && list.size() != 0) {
                return ResponseUtil.success("获取数据成功！", list);
            }
            return ResponseUtil.error("获取数据异常", false);
        }catch (Exception e){
            return ResponseUtil.error(e.getMessage(),null);
        }
    }

    @ApiOperation("根据ID获取单个调查问卷")
    @RequestMapping(value = "/getSurvey", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> getSurvey(@RequestParam(value = "suId", required = false) Integer suId)  {

            SurveyDTO dto=surveyService.findOneSurvey(suId);
            if(dto==null){
                return ResponseUtil.error404("未发现该资源");
            }
            return ResponseUtil.success("ok",dto);
    }

    @ApiOperation("根据ID获取调查问卷,含问题")
    @RequestMapping(value = "/getSurveyQuDetail", method = RequestMethod.GET)
    public ResponseEntity<ResponseVO> getSurveyQuDetail(@RequestParam(value = "suId", required = false) Integer suId) {

        try {
            SurveyQuDetailDTO dto=surveyService.findOneSurveyDetail(suId);
            return ResponseUtil.success("ok",dto);
        }catch (Exception e){
           return ResponseUtil.error(e.getMessage(),null);
        }

    }


}
